
# struct that handles rewards
struct game_reward {

	# sprite, of what the reward looks like
	struct dd_sprite sprite;

	# interval is how often the reward appears
	# counter is how close it is to appearing
	# when counter >= interval, reward is active
	int counter;
	int interval;

	# init the reward as deactivated
	void init() {
		this.sprite.load("dd_logo.png");
		this.sprite.w = 50;
		this.sprite.h = 50;

		this.interval = 100;
		this.counter = 0;
	};

	# if deactivated, advance counter until it activates
	# in which case it moves to a random position on the screen
	void update() {
		if (this.counter < this.interval) {
			this.counter = this.counter +1;
			if (this.counter == this.interval) {
				this.sprite.x = dd_math_rand(DD_GAME_WIDTH  -this.sprite.w);
				this.sprite.y = dd_math_rand(DD_GAME_HEIGHT -this.sprite.h);
			}
		}
	};

	# only draw reward if activated
	void draw() {
		if (this.counter >= this.interval) {
			this.sprite.draw();
		}
	};

}; # reward struct

# main world
struct world_main : dd_world {

	# test sprite
	struct dd_sprite my_sprite;

	# bezier curve points
	struct dd_vector2d bezier1;
	struct dd_vector2d bezier2;
	struct dd_vector2d bezier3;

	# bezier counter
	int counter;

	# current point ( 0 = top left, 1 = top right, 2 = bottom right, 3 = bottom left )
	int point;

	# rewards
	struct game_reward reward1;

	# punishments
	struct dd_sprite punishment1;
	int punishment1_counter;
	int punishment_interval;

	# init sprite
	void init() {

		# start from point 0
		this.point = 0;
		this.counter = 100;

		# first bezier curve has random values
		this.bezier1.x = 0;
		this.bezier1.y = 0;
		this.bezier2.x = DD_GAME_WIDTH -10;
		this.bezier2.y = 0;
		this.bezier3.x = DD_GAME_WIDTH;
		this.bezier3.y = DD_GAME_HEIGHT;

		# first sprite is at the top
		this.my_sprite.load("dd_logo.png");

		this.punishment_interval = 150;
		this.punishment1.load("dd_logo2.png");
		this.punishment1.w = 75;
		this.punishment1.h = 75;
		this.punishment1_counter = 0;

	};

	# update sprite's position
	override void update() {

		# update counter
		this.counter = this.counter +1;

		# counter finished, moved to next curve
		if (this.counter > 100) {

			# reset counter and move to next point
			this.counter = 0;
			this.point = this.point +1;
			if (this.point >= 4) {
				this.point = 0;
			}

			# calculate the first two points of a bezier curve
			this.bezier1.x = this.my_sprite.x +this.my_sprite.w/2;
			this.bezier1.y = this.my_sprite.y +this.my_sprite.h/2;

			# the third point depends on the active point
			if (this.point == 0) {
				this.bezier3.x = 0;
				this.bezier3.y = 0;
			}

			if (this.point == 1) {
				this.bezier3.x = DD_GAME_WIDTH;
				this.bezier3.y = 0;
			}

			if (this.point == 2) {
				this.bezier3.x = DD_GAME_WIDTH;
				this.bezier3.y = DD_GAME_HEIGHT;
			}

			if (this.point == 3) {
				this.bezier3.x = 0;
				this.bezier3.y = DD_GAME_HEIGHT;
			}

			int halfNewx;
			int halfNewy;
			halfNewx = this.bezier1.x +(this.bezier3.x -this.bezier1.x) /2;
			halfNewy = this.bezier1.y +(this.bezier3.y -this.bezier1.y) /2;
			this.bezier2.x = halfNewx +(this.bezier2.x -halfNewx) /2;
			this.bezier2.y = halfNewy +(this.bezier2.y -halfNewy) /2;

		} # counter finished

		# calculate the interpolation of those points
		int finalx;
		finalx = dd_curves_bezier(this.bezier1.x, this.bezier2.x, this.bezier3.x, this.counter);

		int finaly;
		finaly = dd_curves_bezier(this.bezier1.y, this.bezier2.y, this.bezier3.y, this.counter);

		# apply result to sprite
		this.my_sprite.x = finalx -this.my_sprite.w/2;
		this.my_sprite.y = finaly -this.my_sprite.h/2;

		# reward
		this.reward1.update();

		# if reward is active, handle collision
		if (this.reward1.counter == this.reward1.interval) {
			if (this.my_sprite.collide(this.reward1.sprite)) {
				this.reward1.counter = 0;
			}
		}

		# punishment
		if (this.punishment1_counter < this.punishment_interval) {
			this.punishment1_counter = this.punishment1_counter +1;
			if (this.punishment1_counter == this.punishment_interval) {
				this.punishment1.x = dd_math_rand(DD_GAME_WIDTH  -this.punishment1.w);
				this.punishment1.y = dd_math_rand(DD_GAME_HEIGHT -this.punishment1.h);
			}
		}

		# if punishment is active, handle collision
		if (this.punishment1_counter == this.punishment_interval) {
			if (this.my_sprite.collide(this.punishment1)) {
				this.punishment1_counter = 0;
				dd_open_world(world_main_create);
				#dd_game_exit();
			}
		}

	}; # update

	# draw sprite
	override void draw() {
		this.my_sprite.draw();
		this.reward1.draw();
		if (this.punishment1_counter == this.punishment_interval) {
			this.punishment1.draw();
		}
	};

	override void click(int x, int y) {
		if (x >= (DD_GAME_WIDTH/2)) {
			this.counter = 100;
		}
	};

}; # world_main
